#include "fig_bonepart.h"
#include "fighter.h"
#include "fig_player_craft.h"
#include "fig_functions.h"

#include "math/facing.h"


FIGBonePart::FIGBonePart(int pID,
						const std::string& Name,
						float X,
						float Y,
						float Rotation) :    
					m_bInit(false),
					m_Rotation(0),
					m_bShouldDraw(true),
					m_PosResetCounterAfterShoot(0),
					m_RotationBackup(0),
					m_IsOkToExplode(true)

{
	m_ID = pID;
	m_Sprite.Create(Name);
	//if(!m_Sprite.HasMask())
	//	m_Sprite.CreateMask();
	
	m_Size = m_Sprite.m_Size;
	m_PosBackup  = m_Pos = math::Vec2(X,Y);
	m_RotationBackup = m_Rotation = Rotation;	

	m_IsOkToExplode = IsOkToExplode(Name);
}

void	FIGBonePart::Init(const math::Vec2& Pos,
						float Rotation)
{
	if(m_bInit)
		return;

	m_bInit = true;
	m_InitialPos = m_PosBackup = m_Pos = Pos;
	m_InitialRotation = m_RotationBackup = m_Rotation = Rotation;
}	

void	FIGBonePart::Reset()
{
	if(!m_bInit)
		return;

	m_PosBackup = m_Pos = m_InitialPos;
	m_Rotation = m_RotationBackup = m_InitialRotation;
}

bool	FIGBonePart::Transform(float dt , const math::Vec2& Dest, const math::Vec2 Speed)
{
	if(Speed.x == 0 && Speed.y == 0)
		return true;

	math::Vec2 Step = Speed * dt;
	float Remaining = (Dest - m_Pos).Magnitude();

	if(Remaining < Step.Magnitude())
	{
		m_Pos = Dest;
		m_PosBackup = m_Pos;
		return true;
	}
	
	m_Pos += Step;
	return false;
	
}

bool	FIGBonePart::Transform(float dt , float RotationDest, float Speed)
{
	const float COMPARE_LEAST = 0.5f;
	//if(m_Rotation - RotationDest >= -COMPARE_LEAST && m_Rotation - RotationDest <= COMPARE_LEAST)
	//	return true;
	FigFunctions::ToDegree(m_Rotation,RotationDest,Speed * dt);
	
	if(m_Rotation - RotationDest >= -COMPARE_LEAST && m_Rotation - RotationDest <= COMPARE_LEAST)
	{
		m_RotationBackup = m_Rotation;
		return true;
	}
	return false;

	 
}

void	FIGBonePart::Render(const math::Vec2& Offset, float Scale ,float Rotation, COLOR c)
{
	if(!m_bShouldDraw)
		return;
	m_Sprite.m_Color = c;
	m_Sprite.m_Rotation = Rotation + m_Rotation;
	m_Sprite.m_Pos = m_Pos + Offset;
	m_Sprite.m_Size = m_Size * Scale;
	m_Sprite.RenderRotated();
}

void		FIGBonePart::Shoot(float Degree)
{
	
	math::Vec2 Dir	= math::Facing::VectorFromAngle(  Degree);
	m_PosBeforeShoot = m_Pos;
	m_Pos += (Dir * 0.005f);
	m_PosResetCounterAfterShoot = 0.1f;

	
}

void		FIGBonePart::Tick(float dt)
{
    m_Sprite.Tick(dt);
	if(m_PosResetCounterAfterShoot > 0)
	{
		m_PosResetCounterAfterShoot -= dt;
		if(m_PosResetCounterAfterShoot <= 0)
		{
			m_Pos = m_PosBeforeShoot;
		}
	}   

}

void	FIGBonePart::AimToPlayer(const math::Vec2& BonePos,
								 float Speed
								 )
{
	math::Vec2 PlayerPos = FIGPlayerCraft::Instance().GetPos();
	math::Vec2 MyPos = BonePos + m_Pos;

	float Angle = math::Facing::AngleFromTo(MyPos , PlayerPos);
	Angle += 180;
	

	FigFunctions::ToDegree(	m_Rotation,
							Angle,
							Speed
							);
}

bool	FIGBonePart::HitTest(const math::Vec2& BonePos,const math::Vec2& Pos)
{
	return FigFunctions::HitTest(	&m_Sprite, 
								Pos,
								BonePos + m_Pos ,
								m_Size);

	return false;
}

void	FIGBonePart::SetRotation(float R)
{ 
	if(R > 360.0f)
		R -= 360.0f;

	if(R < 0.0f)
		R += 360.0f;


	m_Rotation = R; 
}

bool	FIGBonePart::IsOkToExplode(const std::string& BonePartName)	 const
{


	std::string TestStr = "";


	TestStr = "beast_hand_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "beast_hand_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "beast_wing_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "beast_wing_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "boss_u14_forehand_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "boss_u14_forehand_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "boss_u14_lasergun_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "boss_u14_lasergun_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "boss_u14_wing_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "boss_u14_wing_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "boss_u14_wingbody_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "boss_u14_wingbody_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "hulk_wing_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "hulk_wing_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "hummer_glow_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "hummer_glow_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "hummer_machine_gun_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "hummer_machine_gun_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "mini_boss_hadiez_body";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "mini_boss_hadiez_engine_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "mini_boss_hadiez_engine_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "mini_boss_hadiez_side_backwing_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "mini_boss_hadiez_side_backwing_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "mini_boss_hadiez_side_wing_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "mini_boss_hadiez_side_wing_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "mini_boss_hadiez_wing_left";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "mini_boss_hadiez_wing_right";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "triangle_frame";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "triangle_frame_2";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "triangle_frame_3";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	TestStr = "launcher_laser_body";
	if( BonePartName.compare(TestStr) 	== 0)
		return false;

	


	return true;
}

